let screenWidth = 0

uni.getSystemInfo({
	success: (r) => {
		screenWidth = r.screenWidth
	}
})

let util = {
	// rpx转px
	rpx2px (rpx) {
		let ratio = screenWidth / 750 
		return rpx * ratio
	},
	// 随机字符串
	generateRandomString (length) {
	  const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	  let result = '';
	  for (let i = 0; i < length; i++) {
	    const randomIndex = Math.floor(Math.random() * characters.length);
	    result += characters[randomIndex];
	  }
	  return result;
	},
	// 十进制字符串转Base64字符串
	hex2Base64 (h) {
		var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
		var b64pad = "=";
	    var i;
	    var c;
	    var ret = "";
	    for (i = 0; i + 3 <= h.length; i += 3) {
	        c = parseInt(h.substring(i, i + 3), 16);
	        ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
	    }
	    if (i + 1 == h.length) {
	        c = parseInt(h.substring(i, i + 1), 16);
	        ret += b64map.charAt(c << 2);
	    }
	    else if (i + 2 == h.length) {
	        c = parseInt(h.substring(i, i + 2), 16);
	        ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
	    }
	    while ((ret.length & 3) > 0) {
	        ret += b64pad;
	    }
	    return ret;
	},
	sleep (millisecond) {
		return new Promise((res) => {
			setTimeout(() => {
				res()
			}, millisecond)
		})
	},
	// 随机范围整数
	getRandomInt (min, max) {
		min = Math.ceil(min); // 确保min是整数
		max = Math.floor(max); // 确保max是整数
		return Math.floor(Math.random() * (max - min + 1)) + min; // 返回介于min和max之间的整数
	}
}

function initUtil (app) {
	app.config.globalProperties.$util = util
}


export { initUtil }